<?php

namespace vv\service;

use Exception;

class Shop
{
    public static function supply_enable($shop_id)
    {
        $shop = \vv\model\Shop::get($shop_id);
        $shop->save([
            'shoptype' => 2,
            'apply'    => 0
        ]);
        $shop->config->save([
            'supply_state' => 1,
            'supply_apply' => 0
        ]);
    }

    public static function supply_disable($shop_id)
    {
        $shop = \vv\model\Shop::get($shop_id);
        $shop->save([
            'shoptype' => 1,
            'apply'    => 0
        ]);
        $shop->config->save([
            'supply_state' => 0,
            'supply_apply' => 0
        ]);
    }

    public static function supply_apply($shop_id)
    {
        $shop = \vv\model\Shop::get($shop_id);
        if (!$shop) {
            throw new Exception('店铺不存在');
        }
        if ($shop['shoptype'] == 2) {
            throw new Exception('已是供应商');
        }
        if ($shop['apply'] == 1) {
            throw new Exception('申请正在处理中,请耐心等待.');
        }
        $shop->save(['apply' => 1]);
        $shop->config->save([
            'supply_apply' => 1
        ]);
    }
}